package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.addChuFang

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.fragment.BaseFragment
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.FragmentMyCustomChuFangBinding
import com.lizardmind.haofudoctor.databinding.ItemChuFangBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.ChooseChuFangEvent
import com.lizardmind.haofudoctor.other.ChuFangBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_my_custom_chu_fang.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by Administrator on 2017/11/15.
 * 添加处方——我创建的处方Fragment
 */
class MyCustomChuFangFragment : BaseFragment() {
    private var page=1
    private val count=10
    val list= arrayListOf<ChuFangBean>()

    private val adapter by lazy { BaseAdapter<ChuFangBean,ItemChuFangBinding>(R.layout.item_chu_fang,list) }

    private val binding by lazy { getDataBinding<FragmentMyCustomChuFangBinding>(R.layout.fragment_my_custom_chu_fang) }
    override fun onCreatRootView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {

    return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        tvCustomChuFang.onClick { startActivity(Intent(activity,CustomChuFangActivity::class.java)) }

        rvMyCustomYaoFang.layoutManager=LinearLayoutManager(activity)
        rvMyCustomYaoFang.adapter=adapter
        adapter.onBind { itemBingding, position, data ->
            itemBingding.rLayoutItem.onClick {
                EventBus.getDefault().post(ChooseChuFangEvent(list[position]))
                activity.finish()
            }
        }

        refreshLayout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                page=1
                list.clear()
                getYaoFang(1)
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                page++
                getYaoFang(2)
            }
        })

    }

    @SuppressLint("MissingSuperCall")
    override fun onStart() {
        super.onStart()
        refreshLayout.startRefresh()
    }

    //加载我的药方  1=刷新，2=加载
    private fun getYaoFang(flag:Int){
        postApi<Request<List<ChuFangBean>>>(hashMapOf("action" to "cyyao",
                "token" to DoctorInfo.token,
                "type" to "2",//自己创建的处方type=2
                "p" to page.toString(),
                "count" to count.toString()),
                Consumer {
                    it.dispose(notData = {
                    },data = {
                        list.addAll(it)
                        adapter.notifyDataSetChanged()
                    })
                }, Action {
            if (flag==1){
                refreshLayout.finishRefreshing()
            }else if (flag==2){
                refreshLayout.finishLoadmore()
            }
        })
    }

}